package com.xinqi.modules.course.comment.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.comment.dto.req.*;
import com.xinqi.modules.course.comment.dto.rsp.CommentResultDTO;
import com.xinqi.modules.course.comment.service.CommentService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * APP -- 评论点赞
 *
 * @author: yyzhen
 * @date: 2023/03/20
 */
@Tag(name = "App-评论点赞", description = "评论点赞")
@RestController
@RequestMapping("/app/comment")
@RequiredArgsConstructor
public class CommentAppController {

    private final CommentService commentService;

    /**
     * 评论 -- 分页（讨论时注意评论后方可查看他人评论条件）
     */
    @GetMapping("/page")
    public R<PageInfo<CommentResultDTO>> page(Param pageable, CommentQueryDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        PageInfo<CommentResultDTO> page = commentService.page(pageable, dto);
        return R.data(page);
    }

    /**
     * 评论 -- 评论
     */
    @PostMapping("/comment")
    public R<CommentResultDTO> comment(@Validated @RequestBody CommentCreateDTO dto) {
        return R.data(commentService.comment(dto, LoginHelper.getXqLoginUser()), MessageUtils.message("course.comment.message.comment.success"));
    }

    /**
     * 评论 -- 回复
     */
    @PostMapping("/reply")
    public R<CommentResultDTO> reply(@Validated @RequestBody CommentReplyDTO dto) {
        return R.data(commentService.reply(dto, LoginHelper.getXqLoginUser()), MessageUtils.message("course.comment.message.reply.success"));
    }

    /**
     * 评论 -- 点赞 / 取消点赞
     */
    @PostMapping("/give")
    public R<Void> give(@RequestBody CommentGiveCreateDTO dto) {
        return commentService.give(dto, LoginHelper.getXqLoginUser());
    }

    /**
     * 评论 -- 删除
     */
    @PostMapping("/remove")
    public R<Void> remove(@RequestBody CommentDeleteDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (commentService.remove(dto)) {
            return R.ok(MessageUtils.message("course.comment.message.remove.success"));
        }
        return R.fail(MessageUtils.message("course.comment.message.remove.fail"));
    }

    /**
     * 获取当前的总评论数与总点赞数
     *
     * @Title:
     * @MethodName: count
     * [type, contentId]
     * @Return com.xinqi.common.base.response.R<java.util.Map < java.lang.String, java.lang.Integer>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/4/24 21:29
     */
    @PostMapping("/count")
    public R<Map<String, Integer>> count(@RequestParam(value = "type") Integer type,
                                         @RequestParam(value = "contentId") Long contentId,
                                         @RequestParam(value = "times", required = false) Integer times) {
        return R.data(commentService.selectCountByTypeAndContentId(type, contentId, times));
    }

}
